import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter_common_app/provider_model/audio_model.dart';
import 'package:flutter_common_app/router/route_util.dart';
import 'package:flutter_common_app/utils/common_util/log_util.dart';
import 'package:flutter_common_app/widgets/audio_bottom/play_song_list_dialog.dart';
import 'package:flutter_common_app/widgets/custom_image/custom_image.dart';
import 'package:flutter_common_app/widgets/empty_view/h_empty_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class AudioBottomWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer(builder: (BuildContext context, AudioModel model, child) {
      return Positioned(
        bottom: 0,
        left: 0,
        child: Visibility(
          visible: model.audioBottomWidgetStatus && model.playList.length > 0,
          child: GestureDetector(
            behavior: HitTestBehavior.opaque,
            onHorizontalDragUpdate: (DragUpdateDetails details) {
              LogUtil.v(details);
              if (details.delta.dx < -50) {
                model.nextPlay();
                return;
              }
              if(details.delta.dx >50){
                model.lastPlay();
                return;
              }
            },
            onTap: (){
              RouteUtil.toPlayDetailPage(context);
            },
            child: Container(
              width: 750.w,
              height: 100.w,
              padding: EdgeInsets.symmetric(horizontal: 30.w),
              decoration: BoxDecoration(
                boxShadow: [
                  BoxShadow(
                    offset: Offset(2, 1),
                    color: Colors.black.withOpacity(0.1),
                    blurRadius: 5.w,
                  ),
                ],
                color: Colors.white,
              ),
              child: Row(children: [
                CustomImage.network(
                  '${model.currentSongData?.picUrl}?param=50y50',
                  width: 70.w,
                  borderRadius: BorderRadius.all(
                    Radius.circular(
                      10.w,
                    ),
                  ),
                ),
                HEmptyView(15.w),
                Expanded(
                  child: Text.rich(
                    TextSpan(
                      children: [
                        TextSpan(
                          text: model.currentSongData?.name ?? '私人FM',
                          style: TextStyle(
                            fontSize: 28.sp,
                            decoration: TextDecoration.none,
                            color: Colors.black,
                            fontWeight: FontWeight.normal,
                          ),
                        ),
                        TextSpan(
                          text: '-${model.currentSongData?.artists}',
                          style: TextStyle(
                            fontSize: 20.sp,
                            decoration: TextDecoration.none,
                            color: Colors.black54,
                            fontWeight: FontWeight.normal,
                          ),
                        ),
                      ]
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                HEmptyView(20.w),
                GestureDetector(
                  onTap: () {
                    model.togglePlay();
                  },
                  behavior: HitTestBehavior.opaque,
                  child: Image.asset(
                    model.playerState == AudioPlayerState.PLAYING
                        ? 'images/pause.png'
                        : 'images/play.png',
                    width: 45.w,
                    height: 45.w,
                    fit: BoxFit.cover,
                  ),
                ),
                HEmptyView(30.w),
                GestureDetector(
                  onTap: () {
                    showDialog(
                      context: context,
                      builder: (context) => PlayListDialog(true),
                      barrierDismissible: true,
                    );
                  },
                  behavior: HitTestBehavior.opaque,
                  child: Image.asset(
                    'images/list.png',
                    width: 45.w,
                    height: 45.w,
                    fit: BoxFit.cover,
                  ),
                ),
              ]),
            ),
          ),
        ),
      );
    });
  }
}

